package com.hippo.core.core.manager

import android.os.Parcelable
import com.tencent.mmkv.MMKV

/**
 * APP 数据存储
 */
object AppStore {

    private val defaultMMKV: MMKV = MMKV.defaultMMKV(MMKV.SINGLE_PROCESS_MODE, "hippo")

    @JvmStatic
    fun save(key: String, data: Any?) {
        when (data) {
            is String -> {
                defaultMMKV.encode(key, data)
            }
            is Int -> {
                defaultMMKV.encode(key, data)
            }
            is Boolean -> {
                defaultMMKV.encode(key, data)
            }
            is Float -> {
                defaultMMKV.encode(key, data)
            }
            is Long -> {
                defaultMMKV.encode(key, data)
            }
            is Double -> {
                defaultMMKV.encode(key, data)
            }
            is ByteArray -> {
                defaultMMKV.encode(key, data)
            }
            is Parcelable -> {
                defaultMMKV.encode(key, data)
            }
            else -> {
                defaultMMKV.encode(key, data.toString())
            }
        }
    }

    @JvmStatic
    fun saveSet(key: String, sets: Set<String>) {
        defaultMMKV.encode(key, sets)
    }

    @JvmStatic
    fun saveParcelable(key: String, obj: Parcelable) {
        defaultMMKV.encode(key, obj)
    }

    @JvmStatic
    fun readInt(key: String, defaultValue: Int = 0): Int {
        return defaultMMKV.decodeInt(key, defaultValue)
    }

    @JvmStatic
    fun readDouble(key: String, defaultValue: Double = 0.0): Double {
        return defaultMMKV.decodeDouble(key, defaultValue)
    }

    @JvmStatic
    fun readLong(key: String, defaultValue: Long = 0): Long {
        return defaultMMKV.decodeLong(key, defaultValue)
    }

    @JvmStatic
    fun readBoolean(key: String, defaultValue: Boolean): Boolean {
        return defaultMMKV.decodeBool(key, defaultValue)
    }

    @JvmStatic
    fun readString(key: String, defaultValue: String? = null): String? {
        return defaultMMKV.decodeString(key, defaultValue)
    }

    @JvmStatic
    fun readFloat(key: String, defaultValue: Float = 0.0f): Float {
        return defaultMMKV.decodeFloat(key, defaultValue)
    }

    @JvmStatic
    fun readBytes(key: String, defaultValue: ByteArray?=null): ByteArray? {
        return defaultMMKV.decodeBytes(key, defaultValue)
    }

    @JvmStatic
    fun readStringSet(key: String, defaultValue: Set<String>? = null): Set<String>? {
        return defaultMMKV.decodeStringSet(key, defaultValue)
    }

    @JvmStatic
    fun <T : Parcelable> readParcelable(key: String, tClass: Class<T>): T? {
        return defaultMMKV.decodeParcelable(key, tClass)
    }


    @JvmStatic
    fun removeKey(key: String) {
        defaultMMKV.removeValueForKey(key)
    }

    @JvmStatic
    fun contains(key: String): Boolean {
        return defaultMMKV.contains(key)
    }

    @JvmStatic
    fun clearAll() {
        defaultMMKV.clearAll()
        defaultMMKV.async()
    }

    @JvmStatic
    fun containKey(key: String):Boolean{
        return defaultMMKV.contains(key)
    }

    @JvmStatic
    fun commit(){
        defaultMMKV.async()
    }


}